//package com.lanou3g.bookstore.order.web.servlet;
//
//import com.lanou3g.bookstore.book.domain.Book;
//import com.lanou3g.bookstore.cart.domain.Cart;
//import com.lanou3g.bookstore.cart.domain.CartItem;
//import com.lanou3g.bookstore.order.domain.Order;
//import com.lanou3g.bookstore.order.domain.OrderItem;
//import com.lanou3g.bookstore.order.service.OrderService;
//import com.lanou3g.bookstore.user.domain.User;
//import com.lanou3g.bookstore.util.upload.UU;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//@WebServlet(name = "OrderServlet", urlPatterns = "/order.do")
//public class OrderServlet extends HttpServlet {
//    private OrderService orderService = new OrderService();
//
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //提交订单
//        Order order = new Order();
//        List<OrderItem> orderItemList = new ArrayList<>();
//        String orderNum = UU.getUUID();
//        Cart cart = (Cart) request.getSession().getAttribute("cart");
//        Map<String, CartItem> cartItems = cart.getCartItemMap();
//        Set<String> bids = cartItems.keySet();
//        for (String bid : bids) {
//            OrderItem orderItem = new OrderItem();
//            String itemNum = UU.getUUID();
//            CartItem cartItem = cartItems.get(bid);
//            Book book = cartItem.getBook();
//            int count = cartItem.getCount();
//            orderItem.setIid(itemNum)
//                    .setCount(count)
//                    .setSubtotal(count * book.getPrice())
//                    .setOid(orderNum)
//                    .setBid(book.getBid())
//                    .setBook(book);
//            orderItemList.add(orderItem);
//        }
//        User user = (User) request.getSession().getAttribute("user");
//        String sumMoney1 = request.getParameter("sumMoney");
//        Double sumMoney = Double.valueOf(sumMoney1);
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat =
//                new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        order.setOid(orderNum)
//                .setOrderTime(simpleDateFormat.format(date))
//                .setTotal(sumMoney)
//                .setState(1)
//                .setUid(user.getUid())
//                //地址先不写
//                .setAddress("")
//                .setOrderItemList(orderItemList);
//        orderService.add(order);
//        request.setAttribute("order", order);
//        cartItems.clear();
//        request.getRequestDispatcher("/jsps/order/desc.jsp")
//                .forward(request, response);
//    }
//
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //获取用户的所有订单
//        User user = (User) request.getSession().getAttribute("user");
//        if (user == null) {
//            response.sendRedirect(request.getContextPath() + "/jsps/user/login.jsp");
//            return;
//        }
//        String oid = request.getParameter("oid");
//        if (oid != null) {
//            //点击付款,按oid查询该订单
//            Order order = orderService.load(oid);
//            request.setAttribute("order", order);
//            request.getRequestDispatcher("/jsps/order/desc.jsp")
//                    .forward(request, response);
//        } else {
//            List<Order> orders = orderService.findByUid(user.getUid());
//            request.setAttribute("orders", orders);
//            request.getRequestDispatcher("/jsps/order/list.jsp")
//                    .forward(request, response);
//        }
//
//    }
//}
